'use strict';

const Processor = require('./base_processor');
const path = require('path');
const LogUtil = require('../lib/util/log_util');
const { ExcelColumns } = require('./base_processor');
const Logger = require('../lib/log/logger');
const RCMSCategoriesStore = require('./store/rcms_categories_store');
const StringUtil = require('./util/string_util');
const OLD_ID_NEW_MAPPING = [
  {
    realValue: 4146,
    value: 'qgfdcsczxzxzzcsjyjlxch20071121qgfdcsczxzxzzcsjyjlxch20071121',
  },
  {
    realValue: 4253,
    value: 'dsjgjznlsjzyjzjndhzxjsycpblh20dsjgjznlsjzyjzjndhzxjsycpblhxg',
  },
  {
    realValue: 4474,
    value: 'dejgjznlsjzyjzjndhzxjsycpblh20tpxw',
  },
  {
    realValue: 4476,
    value: 'dejgjznlsjzyjzjndhzxjsycpblh20xgwj',
  },
  {
    realValue: 4552,
    value: 'gwyxwbgsxwfbh20090106_gwyxwbgsgwyxwbgsxwfbh20090106_gwyxwbgs',
  },
  {
    realValue: 5764,
    value: '2009nzgcswcrhddtbd',
  },
  {
    realValue: 5995,
    value: 'dqpgjjfjmsqxwfbh20100126_bjdqpgjjfjmsqxwfbhxgwj',
  },
  {
    realValue: 6026,
    value: 'yostcwlqdyszyxjzyth20100921_bjyostcwlqdyszyxjzyth20100921_bj',
  },
  {
    realValue: 6112,
    value: '2011wcrtpxw',
  },
  {
    realValue: 6135,
    value: '2011zyyxdstpxw',
  },
  {
    realValue: 6139,
    value: 'jsxcscjgzhytpxw',
  },
  {
    realValue: 6210,
    value: 'wmfuzygzdt',
  },
  {
    realValue: 6211,
    value: 'wmfuzyzyjs',
  },
  {
    realValue: 6226,
    value: '20120116tggqgjtpxw',
  },
  {
    realValue: 6232,
    value: '20120116tggqgjxgwj',
  },
  {
    realValue: 6265,
    value: 'd8jgjlsjzjndhtpxw',
  },
  {
    realValue: 6266,
    value: 'd8jgjlsjzjndhmtbd',
  },
  {
    realValue: 6274,
    value: 'ctcldchytpxw',
  },
  {
    realValue: 6287,
    value: '2012bfcndqgrjlgghtpxw',
  },
  {
    realValue: 6309,
    value: 'ddqzlxzyjs',
  },
  {
    realValue: 6332,
    value: '922wcrxwfbhmtbd',
  },
  {
    realValue: 6334,
    value: '922wcrxwfbhxgwj',
  },
  {
    realValue: 6335,
    value: '922wcrxwfbhhyzl',
  },
  {
    realValue: 6400,
    value: '2014nwcrxwfbhxgwj',
  },
  {
    realValue: 6402,
    value: '2014nwcrxwfbhmtbd',
  },
];
const NEW_NAME_MAPPING_OLD = [
  {
    realValue: '专题信息',
    value: '专题',
  },
];

class SeminarProcessor extends Processor {
  constructor(filePath) {
    super(filePath, 'SeminarProcessor');
    this.logger = new Logger('SeminarProcessor', LogUtil.getFileLogWriter('SeminarProcessor', path.basename(this.outputFilePath)));
  }

  async process() {
    this.logger.clear();
    const oldDbRows = await RCMSCategoriesStore.list();

    const excelRows = this.read();

    if (excelRows && excelRows.length > 0) {
      for (const excelRow of excelRows) {
        const exactlyOldId = StringUtil.getRealParentName(excelRow[ExcelColumns.NEW_COLUMN_CODE], OLD_ID_NEW_MAPPING);
        let oldDdRow = oldDbRows.find(xx => xx.id === exactlyOldId);
        if (!oldDdRow) {
          const oldDdRowMulti = oldDbRows.filter(x => x.path === exactlyOldId
              && x.name === StringUtil.getRealParentName(excelRow[ExcelColumns.NEW_COLUMN].trim(), NEW_NAME_MAPPING_OLD));
          if (oldDdRowMulti.length > 0) {
            let compareNewPath = excelRow[ExcelColumns.NEW_PATH].trim();
            if (compareNewPath.indexOf(' -') !== -1) {
              compareNewPath = excelRow[ExcelColumns.NEW_PATH].replace(/\s+/g, '');
            }
            oldDdRow = oldDdRowMulti.find(p => this.recursionGetFullPath(p.name, p, oldDbRows) === compareNewPath);
          }
        }
        if (!oldDdRow) {
          this.logger.info(`新栏目code未查询到对应的数据:${excelRow[ExcelColumns.NEW_COLUMN_CODE]}`);
          continue;
        }
        excelRow[ExcelColumns.OLD_COLUMN] = this.recursionGetFullPath(oldDdRow.name, oldDdRow, oldDbRows);
        excelRow[ExcelColumns.OLD_COLUMN_ID] = oldDdRow.id;
        excelRow[ExcelColumns.FILTER] = oldDdRow.filter;
      }
      const output = excelRows;
      this.write(output);
      return this.outputFilePath;
    }
  }

  recursionGetFullPath(fullOldPath, matchDbRow, oldDbRows) {
    const findParent = oldDbRows.find(m => m.id === matchDbRow.parentid);
    if (!findParent) {
      return fullOldPath;
    }

    fullOldPath = findParent.name + '-' + fullOldPath;
    return this.recursionGetFullPath(fullOldPath, findParent, oldDbRows);
  }
}

module.exports = SeminarProcessor;
